%::PROVIDES=(
	name	=> ["HANDLE_ALERT"],
	desc	=> ["Handle HANDLE_ALERT requests"],
	type	=> "request",
	code	=> \&::do_handle_alert
);

#########################################################################
# The alert expects the following argument and is mandatory:		#
# TYPE : This can be any of: APP_START[x],APP_STOP[x],APP_FAIL[x] or	#
# 	  CLUSTER_START[x], CLUSTER_STOP[x], CLUSTER_PARTITIONED[x],	#
# 	  NODE_JOIN[x], NODE_LEAVE[x], NODE_FAIL[x]			#
#									#
# For NODE requests the following arguments are expected:		#
# NODE - the name of the node in question.				#
# For CLUSTER requests:							#
# None 									#
# For APP requests:							#
# APP - The name of the application.					#
#									#
# The cluster configuration must contain details of how the alert	#
# is handled, but for now just an email to root will be generated.	#
#########################################################################
{
my $mailer;

sub ::do_handle_alert {
my $args=shift;
my ($h,@node,$app);
my @T=qw(
	APP_START APP_STOP APP_FAIL
	CLUSTER_START CLUSTER_STOP CLUSTER_PARTITIONED CLUSTER_UNPARTITIONED
	NODE_JOIN NODE_LEAVE NODE_FAIL
	);

	if(!exists($xml->{global}->{alert_to})) {
		return "OK";
	}
	my $email_to=$xml->{global}->{alert_to};
	my $date_format='%d-%m-%Y %H:%M:%S';
	if(exists($xml->{global}->{alert_date_format}) &&
	   defined($xml->{global}->{alert_date_format})) {
		$date_format=$xml->{global}->{alert_date_format};
	}
	my $date=strftime($date_format,localtime(time));
	if(!defined($email_to)) {
		$email_to="root";
	}
	$email_to =~ s/\s+//g;
	if(!length($email_to)) {
		$email_to="root";
	}
	if(!exists($args->{TYPE})) {
		logwarn("HANDLE_ALERT missing TYPE argument");
		return "MISSING_ARG";
	}

	$h=hostname;

	if(! grep {$_ eq $args->{TYPE}} @T) {
		logwarn("HANDLE_ALERT TYPE is not recognised [$args{TYPE}]");
		return "UNKNOWN_TYPE";
	}

	if($args->{TYPE} =~ /^NODE/) {
		if(!exists($args->{NODE})) {
			logwarn("HANDLE_ALERT TYPE NODE missing NODE argument");
			return "MISSING_ARG";
		}
		$node=$args->{NODE};
	}

	if($args->{TYPE} =~ /^APP/) {
		if(!exists($args->{APP})) {
			logwarn("HANDLE_ALERT TYPE APP missing APP argument");
			return "MISSING_ARG";
		}
		$app=$args->{APP};
	}

	#################################################################
	# Now we check to see what command we can use to send the	#
	# email .							#
	#################################################################
	if(!defined($mailer)) {
		for my $f ("/usr/bin/mailx","/bin/mailx") {
			$mailer=$f if -x $f;
			last if $mailer;
		}
	}
	if(!defined($mailer)) {
		logwarn("Unable to find binary to send an email.");
		return "MISSING_BINARY";
	}

	my $fd;
	open($fd,"|$mailer -s 'CLUSTER_EVENT: $args->{TYPE}' $email_to");
	print $fd "CLUSTER     : $xml->{global}->{name}\n";
	print $fd "EVENT       : $args->{TYPE}\n";
	print $fd "DATE/TIME   : $date\n";
	if($args->{TYPE} =~ /^NODE/) {
		print $fd "NODE        : ",$args->{NODE},"\n";
	} elsif($args->{TYPE} =~ /^APP/) {
		print $fd "APPLICATION : ",$args->{APP},"\n";
	}
	if($args->{TYPE} =~ /^(APP|CLUSTER)/) {
		if(exists($args->{NODE})) {
			print $fd "NODE        : ",$args->{NODE},"\n";
		}
	}
	close($fd);
	return "OK";
}

}

1;
